<?php

class EventoController extends Zend_Controller_Action
{

    protected $_eventos;
    
    public function init()
    {
        $this->_eventos = new Eventos();
    }
    public function indexAction()
    {
    }

    public function detalheAction()
    {
        $id = $this->getRequest()->getParam('id');
        $eventos = $this->_eventos->find($id)->current();

        $select = $this->_eventos->select()->order('perfil','nome');
        
        $grupo = Eventos::getGrupo($eventos->id);
        $participantes = Eventos::getGrupo($eventos->id);

        $this->view->data       = $eventos;
        $this->view->grupo      = $grupo;
        $this->view->participantes = $participantes;
        
    }
    
    public function formAction()
	{
		$id    = Zend_Filter::filterStatic($this->_getParam('id'), 'int');
		$table = new Eventos();
		$form  = new Application_Form_Evento(); 
        $form->setAction('evento/save');
		
		if ($id) {
			$data = $table->find($id)->current()->toArray();
            
            $data['dt_inicio'] = $this->view->FormataData($data['dt_inicio']);
            $data['dt_termino'] = $this->view->FormataData($data['dt_termino']);

            $data['custo_por_tutor'] = Zend_Locale_Format::toFloat(
                    $data['custo_por_tutor'],
                    array('locale'=>'pt_BR', 'precision'=>2)); 

            $data['custo_supervisor'] = Zend_Locale_Format::toFloat(
                    $data['custo_supervisor'],
                    array('locale'=>'pt_BR', 'precision'=>2)); 

			$form->populate($data);
		}
		
		$this->view->form = $form;
	}
	
	public function saveAction()
	{
		if ($this->getRequest()->isPost()) {
			$form  = new Application_Form_Evento(); 
			$table = new Eventos();
			
			if ($form->isValid($this->_getAllParams())) {
				$data = $form->getValues();
				if (isset($data['id']) && $data['id']) {
					$table->update($data, array('id = ?' => $data['id']));
				} else {
					$table->insert($data);
				}
                $cache = Zend_Registry::get('cache');
                $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
                if($data['id']):
    				$this->_redirect('default/evento/detalhe/id/'.$data['id']);
                else:
    				$this->_redirect('default/evento/index');
                endif;
			}
			
			$this->view->form = $form;
			$this->render('form');
		}
	}
	
	public function deleteAction()
	{
		$id = Zend_Filter::filterStatic($this->_getParam('id'), 'int');
		
		if ($id) {
			$table = new Eventos();
			$table->delete(array('id = ?' => $id));
            $cache = Zend_Registry::get('cache');
		    $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
		}
		
		$this->_redirect('default/evento/index');
	}
    
    public function ajaxIndexAction(){
        $this->_helper->layout->disableLayout(); 
        $post = $this->getRequest()->getPost();
        $post = $post['queryString'];
        $cache = Zend_Registry::get('cache');
		if (!$result = $cache->load('eventos_'.$post)) {
            $result = Eventos::getEventos(null,$post);
            $cache->save($result,'eventos_'.$post);
        }
        $this->view->eventos = $result;
    }
    public function ajaxFiltrarAction(){
        $this->_helper->layout->disableLayout(); 
        $post = $this->getRequest()->getPost();        
        $this->view->eventos = Eventos::getEventos('evento like "%'.trim($post['search']).'%"','evento');
        $this->render('ajax-index');
    }
    

}
